1   /*
2    * Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.
8    *
9    * This code is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12   * version 2 for more details (a copy is included in the LICENSE file that
13   * accompanied this code).
14   *
15   * You should have received a copy of the GNU General Public License version
16   * 2 along with this work; if not, write to the Free Software Foundation,
17   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18   *
19   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20   * or visit www.oracle.com if you need additional information or have any
21   * questions.
22   */
23  
24  /**
25   * @test
26   * @bug 6476665 6523403 6733501 7042594
27   * @summary Verifies reading and writing profiles and tags of the standard color
28   * spaces
29   * @run main ReadWriteProfileTest
30   */
31  import java.awt.color.ColorSpace;
32  import java.awt.color.ICC_Profile;
33  import java.util.*;
34  import java.nio.*;
35  import java.util.Hashtable;
36  
37  public class ReadWriteProfileTest implements Runnable {
38      /* Location of the tag sig counter in 4-byte words */
39      final static int TAG_COUNT_OFFSET = 32;
40  
41      /* Location of the tag sig table in 4-byte words */
42      final static int TAG_ELEM_OFFSET = 33;
43  
44      static byte[][] profiles;
45      static int [][] tagSigs;
46      static Hashtable<Integer,byte[]> [] tags;
47  
48      static int [] cspaces = {ColorSpace.CS_sRGB, ColorSpace.CS_PYCC,
49                               ColorSpace.CS_LINEAR_RGB, ColorSpace.CS_CIEXYZ,
50                               ColorSpace.CS_GRAY};
51  
52      static String [] csNames = {"sRGB", "PYCC", "LINEAR_RGB", "CIEXYZ", "GRAY"};
53  
54      static void getProfileTags(byte [] data, Hashtable tags) {
55          ByteBuffer byteBuf = ByteBuffer.wrap(data);
56          IntBuffer intBuf = byteBuf.asIntBuffer();
57          int tagCount = intBuf.get(TAG_COUNT_OFFSET);
58          intBuf.position(TAG_ELEM_OFFSET);
59          for (int i = 0; i < tagCount; i++) {
60              int tagSig = intBuf.get();
61              int tagDataOff = intBuf.get();
62              int tagSize = intBuf.get();
63  
64              byte [] tagData = new byte[tagSize];
65              byteBuf.position(tagDataOff);
66              byteBuf.get(tagData);
67              tags.put(tagSig, tagData);
68          }
69      }
70  
71      static {
72          profiles = new byte[cspaces.length][];
73          tags = new Hashtable[cspaces.length];
74  
75          for (int i = 0; i < cspaces.length; i++) {
76              ICC_Profile pf = ICC_Profile.getInstance(cspaces[i]);
77              profiles[i] = pf.getData();
78              tags[i] = new Hashtable();
79              getProfileTags(profiles[i], tags[i]);
80          }
81      }
82  
83      public void run() {
84          for (int i = 0; i < cspaces.length; i++) {
85              ICC_Profile pf = ICC_Profile.getInstance(cspaces[i]);
86              byte [] data = pf.getData();
87              pf = ICC_Profile.getInstance(data);
88              if (!Arrays.equals(data, profiles[i])) {
89                  System.err.println("Incorrect result of getData() " + "with " +
90                                     csNames[i] + " profile");
91                  throw new RuntimeException("Incorrect result of getData()");
92              }
93  
94              for (int tagSig : tags[i].keySet()) {
95                  byte [] tagData = pf.getData(tagSig);
96                  byte [] empty = new byte[tagData.length];
97                  boolean emptyDataRejected = false;
98                  try {
99                      pf.setData(tagSig, empty);
100                 } catch (IllegalArgumentException e) {
101                     emptyDataRejected = true;
102                 }
103                 if (!emptyDataRejected) {
104                     throw new
105                         RuntimeException("Test failed: empty tag data was not rejected.");
106                 }
107                 pf.setData(tagSig, tagData);
108 
109                 byte [] tagData1 = pf.getData(tagSig);
110 
111                 if (!Arrays.equals(tagData1, tags[i].get(tagSig)))
112                 {
113                     System.err.println("Incorrect result of getData(int) with" +
114                                        " tag " +
115                                        Integer.toHexString(tagSig) +
116                                        " of " + csNames[i] + " profile");
117 
118                     throw new RuntimeException("Incorrect result of " +
119                                                "getData(int)");
120                 }
121             }
122         }
123     }
124 
125     public static void main(String [] args) {
126         ReadWriteProfileTest test = new ReadWriteProfileTest();
127         test.run();
128     }
129 }